package edu.hawaii.myisern.model;

import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Holds ID string value for an XML record.
 * 
 * @author Lisa Chen
 * @author Randy Cox
 */
public class Id implements Comparable {

  /** ID value. */
  private String id;

  /**
   * Creates ID.
   * 
   */
  public Id() {
    this.id = "";
  }

  /**
   * Creates ID from passed in name string.
   * 
   * @param name to create ID from.
   */
  public Id(String name) {
    this.id = build(name);
  }

  /**
   * Build Id from a name. Replaces spaces with underscore, trims and converts
   * to upper case.
   * 
   * @param name Name to build Id from.
   * @return Id
   */
  public static String build(String name) {
    String result = name;
    if (name == null) {
      result = "";
    }
    result = result.trim();
    result = result.replaceAll(" ", "_");
    result = result.toUpperCase();
    return result;
  }

  /**
   * Retrieves Id from class.
   * 
   * @return the id
   */
  public String getId() {
    return id;
  }

  /**
   * Sets ID from a passed in name.
   * 
   * @param name the id to set
   */
  public void setId(String name) {
    this.id = Id.build(name);
  }

  /**
   * Provides ID string.
   * 
   * @return ID string
   */
  @Override
  public String toString() {
    return id;
  }

  /**
   * Checks if a ID strings contents are equal.
   * 
   * @param o Object to compare the this object with
   * @return True if this objects ID is equal to the to passed in object's x ID
   */
  @Override
  public boolean equals(Object o) {
    boolean isEqual = false;
    if (o instanceof Id) {
      Id id = (Id) o;
      if (id.toString().contentEquals(this.id)) {
        isEqual = true;
      }
    }
    return isEqual;
  }
  
  /**
   * Hashcode creation.
   * @return hash code.
   */
  @Override
  public int hashCode() {
    return id.hashCode();
  }
  /**
   * Compare to other objects.
   * @param id The id object to compare to.
   * @return Interger value of comparison.
   */
  public int compareTo(Object id) {
    // TODO Auto-generated method stub
    return this.id.compareTo(id.toString());
  }
  /**
   * Create an id for a researcher object.
   * @param r researcher object.
   * @return Id object to that researcher.
   */
  public static Id getId(Researcher r) {
    return new Id(r.getName());
  }
  /**
   * Create an id for a organization object.
   * @param o organization object.
   * @return Id object to that organization.
   */
  public static Id getId(Organization o) {
    return new Id(o.getName());
  }
  /**
   * Create an id for a collaboration object.
   * @param c collaboration object.
   * @return Id object to that collaboration.
   */
  public static Id getId(Collaboration c) {
    return new Id(c.getName());
  }  
  
  
}
